#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
package ${package}.biz.util;

import org.springframework.util.StringUtils;
import org.springframework.web.util.HtmlUtils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.URLEncoder;

/**
 * Created by well on 16/6/15.
 */
public class StringUtil {


    public static  boolean startWith(Object org,Object str){
        if(org == null || str == null){
            return false;
        }
        if(!StringUtils.hasText(org.toString())){
            return false;
        }
        if(!StringUtils.hasText(str.toString())){
            return false;
        }
        return String.valueOf(org).startsWith(String.valueOf(str));
    }

    public static  boolean hasText(Object org){
        if(org == null){
            return false;
        }
        return StringUtils.hasText(org.toString());
    }

    public static  String htmlEscape(String str){
        if(str == null){
            return null;
        }
        return HtmlUtils.htmlEscape(str);
    }

    public static  String array2str(String[] array){
        if(array == null){
            return null;
        }
        StringBuilder buf = new StringBuilder();
        for(String s:array){
            buf.append(s).append(",");
        }
        if(buf.length()>0){
            return buf.substring(0,buf.length()-1);
        }
        return buf.toString();
    }

    public static  String urlEncode(String str){
        if(str == null){
            return null;
        }
        try {
            return URLEncoder.encode(str, "utf-8");
        }catch(Exception e){

        }
        return str;
    }

    public static  String replaceAll(String str,String match,String matchTo){
        if(str == null){
            return null;
        }
        return str.replaceAll(match, matchTo);
    }

    public static String filter(String str){
        if(str == null){
            return null;
        }
        return str.replaceAll("[${symbol_escape}${symbol_escape}x00-${symbol_escape}${symbol_escape}x08${symbol_escape}${symbol_escape}x0b-${symbol_escape}${symbol_escape}x0c${symbol_escape}${symbol_escape}x0e-${symbol_escape}${symbol_escape}x1f]", "");
    }

    public  static String string2Json(String s) {
        StringBuilder sb = new StringBuilder();
        char[] chars = s.toCharArray();
        for (int i = 0; i < s.length(); i++) {
            char c = chars[i];
            switch (c) {
//                case '${symbol_escape}${symbol_escape}':
//                    sb.append("${symbol_escape}${symbol_escape}${symbol_escape}${symbol_escape}");
//                    break;
                case '/':
                    sb.append("${symbol_escape}${symbol_escape}/");
                    break;
//                case '${symbol_escape}b':
//                    sb.append("${symbol_escape}${symbol_escape}b");
//                    break;
//                case '${symbol_escape}f':
//                    sb.append("${symbol_escape}${symbol_escape}f");
//                    break;
//                case '${symbol_escape}n':
//                    sb.append("${symbol_escape}${symbol_escape}n");
//                    break;
//                case '${symbol_escape}r':
//                    sb.append("${symbol_escape}${symbol_escape}r");
//                    break;
//                case '${symbol_escape}t':
//                    sb.append("${symbol_escape}${symbol_escape}t");
//                    break;
                default:
                    if ((c >= 0 && c <= 31) || c == 127) {

                    } else {
                        sb.append(c);
                    }
                    break;
            }
        }
        return sb.toString();
    }

    public static String jsonDecode(String str){
        if(str == null){
            return null;
        }

        return str.replaceAll("${symbol_escape}${symbol_escape}${symbol_escape}${symbol_escape}/","/");
    }

    /**
     *     把三个字符串按穿插着flag拼接
     */
    public static String appendFlag(String s1, String s2, String s3, String flag){
        String result = "";
        if(s1 != null){
            if(result.equals("")) {
                result = result  + s1;
            }else {
                result = result + flag + s1;
            }
        }
        if(s2 != null){
            if(result.equals("")) {
                result = result  + s2;
            }else {
                result = result + flag + s2;
            }
        }
        if(s3 != null){
            if(result.equals("")) {
                result = result  + s3;
            }else {
                result = result + flag + s3;
            }
        }
        return result;
    }


    public static String appendFlag2(String[] lists, String flag) {
        String result = "";
        if(lists == null){
            return "";
        }
        for (String list : lists) {
            if(result.equals("")) {
                result = list;
            }else{
                result = result + flag + list;
            }
        }
        return result;
    }

    public static String toString(Object object) {
        StringBuffer sb = new StringBuffer();
        sb.append(object.getClass().getName() + ": ");
        Field[] fields = object.getClass().getDeclaredFields();
        Class thisClass = object.getClass();
        for (int i = 0; i < fields.length; ++i) {
            Field field = fields[i];
            String fieldName = field.getName();
            String getMethod = "get" + fieldName.substring(0,1).toUpperCase()
                    + fieldName.substring(1);
            try {
                Method thisMethod = thisClass.getMethod(getMethod, new Class[] {});
                sb.append(fieldName + "=" + thisMethod.invoke(object, new Object[] {}) + ", ");
            } catch (Exception e) {
                sb.append(getMethod + " is not exist, ");
            }
        }
        return sb.toString();
    }


    public static  String hidePhone(Object phone){
        if(phone == null){
            return null;
        }
        if(!StringUtils.hasText(phone.toString())){
            return null;
        }
        String p = phone.toString();
        if(p.length()!=11){
            return p;
        }
        String str1 = p.substring(0, 3);
        String str3 = p.substring(7);

        return str1+"****"+str3;
    }

    public static void main(String[] args){
        String test = "http:${symbol_escape}${symbol_escape}/${symbol_escape}${symbol_escape}/dev.assets.morelook.cn${symbol_escape}${symbol_escape}/d1ad4451ea4f4ea8b20e59407f465ffc.jpg";
        System.out.println(hidePhone("13512345678"));
    }
}
